package com.reloki.client;

import com.reloki.shared.FieldVerifier;
import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GmailGadgetTest implements EntryPoint {

	// Use the implementation of Auth intended to be used in the GWT client app.
	private static final Auth AUTH = Auth.get();

	public void onModuleLoad() {

		addDailymotionAuth();
	}

	private static final String DAILYMOTION_AUTH_URL = "https://github.com/login/oauth/authorize";

	// This app's personal client ID assigned by the Dailymotion Developer App
	// (http://www.dailymotion.com/profile/developer/new).
	private static final String DAILYMOTION_CLIENT_ID = "8830365b0bebb11b76a3";

	// Adds a button to the page that asks for authentication from DailyMotion.
	// Note that Dailymotion does not allow localhost as a redirect URL, so while
	// this code will work when hosted, it will not work when testing locally.
	private void addDailymotionAuth() {
		// Since the auth flow requires opening a popup window, it must be started
		// as a direct result of a user action, such as clicking a button or link.
		// Otherwise, a browser's popup blocker may block the popup.
		Button button = new Button("Authenticate with Github");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final AuthRequest req = new AuthRequest(DAILYMOTION_AUTH_URL, DAILYMOTION_CLIENT_ID);
				AUTH.login(req, new Callback<String, Throwable>() {
					@Override
					public void onSuccess(String token) {
						Window.alert("Got an OAuth token:\n" + token + "\n" + "Token expires in " + AUTH.expiresIn(req) + " ms\n");
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error:\n" + caught.getMessage());
					}
				});
			}
		});
		RootPanel.get().add(button);
	}
}
